<template>
<!-- 保单录入-再保信息录入页面 -->
    <div class="fillcontain">
        <el-form :model="searchParams" :rules="rules" ref="ruleForm" label-width="100px" class="search_container">
            <el-row>
                <el-col :span="6">
                    <el-form-item label="保单号码" prop="chdrnum">
                        <el-input v-model="searchParams.chdrnum" @change="clearCondition('chdrnum')" placeholder="保单号码" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="被保人姓名" prop="surname">
                        <el-input v-model="searchParams.surname" @change="clearCondition('surname')" placeholder="被保人姓名" clearable></el-input>
                    </el-form-item>
                </el-col>
                 <el-col :span="6">
                    <el-form-item label="客户号" prop="lifcnum">
                        <el-input v-model="searchParams.lifcnum" @change="clearCondition('lifcnum')" placeholder="客户号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6" class="text_center">
                    <el-button type="primary" icon="el-icon-search" @click="subGetList"></el-button>
                    <el-button type="primary" plain  icon="el-icon-refresh" @click="resetSearch"></el-button>
                </el-col>
            </el-row>
        </el-form>
        <div class="contain">
             <!-- 操作按钮 -->
                <el-row> 
                    <el-col :span="24" class="pt5">
                        <el-button type="danger" plain @click="hrefOperation">再保操作</el-button>
                        <el-button   v-if="disabledAdd" disabled style="">增加</el-button>
                        <el-button type="danger" plain  v-else @click="accessionOnclik">增加</el-button>
                    </el-col>
                </el-row>
          <div class="table_container mt20">
            <el-table
                 v-loading="loading"
                 :data="tableData"
                  @row-click="selectRow"
                 highlight-current-row
                 header-cell-class-name="table-header-class"
                 style="width:100%">
                <el-table-column
                width="120"
                    property="sqno"
                    label="序号"
                    align='center'
                    show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                    width="120"
                   property="chdrnum"
                   label="保单号"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                 width="120"
                   property="surname"
                   label="被保人"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column>
                 <el-table-column
                 width="120"
                   property="lifcnum"
                   label="客户号"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="cltsex"
                   label="性别"
                   align='center'
                   :formatter="sexFormatter"
                   show-overflow-tooltip>
                </el-table-column> 
                  <el-table-column
                    width="120"
                   property="anbAtCcd"
                   label="年龄"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                width="120"
                   property="statcode"
                   label="保单状态"
                   align='center'
                   show-overflow-tooltip>
                   </el-table-column>
                <el-table-column
                 width="120"
                   property="mortcls"
                   label="职级"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="crtable"
                   label="分保类型"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="sumins"
                   label="保额"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="rscompcode"
                   label="分保公司"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="riraree"
                   label="分保比例"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="bmntdesc"
                   label="再保意见"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="effdate"
                   label="分保生效日"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="validflag"
                   label="有效标志"
                   align='center'
                   :formatter="validflagFormatter"
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="cncldesc"
                   label="作废意见"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column>
                <el-table-column
                 width="120"
                   property="crtuser"
                   label="记录人员"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="strdate"
                   label="记录日期"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="chaguser"
                   label="作废人员"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
                <el-table-column
                 width="120"
                   property="enddate"
                   label="作废日期"
                   align='center'
                   show-overflow-tooltip>
                </el-table-column> 
            </el-table>
           <el-row>
                <el-col :span="24">
                    <div class="pagination">
                        <el-pagination
                            background
                            v-if='paginations.total > 0'
                            :page-sizes="paginations.pageSizes"
                            :page-size="paginations.pageSize"
                            :layout="paginations.layout"
                            :total="paginations.total"
                            :current-page='paginations.pageIndex'
                            @current-change='handleCurrentChange'
                            @size-change='handleSizeChange'>
                        </el-pagination>
                    </div>
                </el-col>
            </el-row>
          </div>
        </div>

    </div>
</template>

<script>
    import { mapState, mapGetters } from 'vuex'
    import constant from '@/utils/constant';
    import * as api from "@/api/policyRecord/reinsurance/reinsuranceInfo.js";
    import {isCntainChinese,isSpecialCharacters,isNotAllNumber} from '@/api/refund/RefundCommon';
    export default {
        data(){
            return {
                //初始禁用添加按钮
                disabledAdd: true,
                isAddFlag:false,
                // 查询条件
                rules: {
                    lifcnum: [],
                    chdrnum: [],
                    surname: []
                },
                searchParams:{
                    chdrnum:'', //客户号
                    surname:'', //客户姓名
                    lifcnum: '', // 证件号码
                },
                inseagnVo: {
                    is_applnum: '',
                    ls_coy: '',
                    s_statcode: '',
                    s_cltnum: '',
                    s_mortcls: '',
                    s_anb_at_ccd: '',
                    s_surname: '',
                    s_cltsex: '',
                    statcode1: '',
                },
                selectRowData: {},
                tableData: [],
                loading:false,
              //需要给分页组件传的信息
                paginations: {
                    total: 0,        // 总数
                    pageIndex: 1,  // 当前位于哪页
                    pageSize: 10,   // 1页显示多少条
                    pageSizes: [5, 10, 15, 20],  //每页显示多少条
                    layout: "total, sizes, prev, pager, next, jumper"   // 翻页属性
                },
                pickerOptions1: {
                  disabledDate(time) {
                    return time.getTime() > Date.now();
                  }
                }
            }
        },
        methods: {
            // 再保操作
            hrefOperation(){
                let chdrnum = this.selectRowData.chdrnum;
                if(!chdrnum){
                    this.$message({
                        message: '请选择操作的数据！',
                        type: 'error'
                    });
                    return false
                }
                this.$router.push({ path:'/home/policyRecord/reinsuranceOperation/'+chdrnum})
            },
            // 查询列表
            getList(){
                this.paginations.total=0;
                this.tableData=[];
                let para = {
                    "chdrnum": this.searchParams.chdrnum,
                    "surname": this.searchParams.surname,
                    "lifcnum": this.searchParams.lifcnum,
                    "pageSize": this.paginations.pageSize,
                    "pageNumber": this.paginations.pageIndex,
                }

                if(para.chdrnum && isNotAllNumber(para.chdrnum)) {
                    this.$message.error("请正确输入保单号！");
					return;
                }
                if(para.surname && isSpecialCharacters(para.surname)) {
                    this.$message.error("请正确输入被保人姓名！");
					return;
                }
                if(para.lifcnum && isNotAllNumber(para.lifcnum)) {
                    this.$message.error("请正确输入客户号！");
					return;
                }


                this.$store.dispatch('loading/CHANGE_LOADING',true)
                api.getReinsureList(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS && res.data && res.data.urls.list.length){
                      this.paginations.total = res.data.urls.total;
                      this.tableData = res.data.urls.list;
                    } else {
                      this.message("error",res.resultMsg);
                      this.disabledAdd = false;
                    }


                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            // 每页多少条切换
            handleSizeChange(pageSize) {
               this.paginations.pageSize = pageSize;
               this.getList();
            },
            // 上下分页
            handleCurrentChange(page) {
               this.paginations.pageIndex = page;
               this.getList();
            },
            // 根据条件查询列表
            subGetList(){
                this.paginations.pageIndex=1;
                this.getList();
            },
            // 清除查询条件
            resetSearch(){
                this.searchParams.chdrnum = '';
                this.searchParams.surname = '';
                this.searchParams.lifcnum = '';
            },
            //根据输入先后清空其他查询条件
            clearCondition(id){
                if(id=='chdrnum'){
                    this.searchParams.surname = "";
                    this.searchParams.lifcnum = "";
                }
                if(id=='surname'){
                    this.searchParams.chdrnum = "";
                    this.searchParams.lifcnum = "";
                }
                if(id=='lifcnum'){
                    this.searchParams.chdrnum = "";
                    this.searchParams.surname = "";
                }
            },
            sexFormatter(row) {
                let result = "";
                if (row.cltsex == 'F') {
                    result = "女性"
                } else if (row.cltsex == 'M') {
                    result = "男性"
                } else if (row.cltsex == 'U') {
                    result = "不明"
                }
                return result;
            },
            validflagFormatter(row) {
                let result = "";
                if (row.validflag == '1') {
                    result = "有效"
                } else if (row.validflag == '2') {
                    result = "作废"
                }
                return result;
            },
            selectRow(row) {
                this.selectRowData = row;
                console.log(this.selectRowData);
            },
            //再添加按钮点击事件
            accessionOnclik() {
                console.log("再添加按钮点击事件")
                let chdrnum = this.searchParams.chdrnum;
				if (chdrnum.length != 8) {
                    this.message('error', '请在查询条件输入八位的保单号码！');
                    return false
                }
                //禁用添加按钮

                let para = {
                    chdrnum: chdrnum
                }
                //新增根据保单号码查询对应的序列号码
                api.checkInseagn(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data > 0) {
                            this.subGetList();
                        } else {
                            this.findApplnumlListByChdrnum(para);
                        }
                    } else{
                      this.message('error', res.resultMsg);
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },

            findApplnumlListByChdrnum(para) {
                let data = [];
                api.findApplnumlListByChdrnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        data = res.data;
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库EUABSTM失败');
                        } else {
                            this.findMaxAgntcoyByApplnum(res.data);
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })



            },
            findMaxAgntcoyByApplnum(data) {
                let is_applnum = data[0].applnum;
                this.inseagnVo.is_applnum = is_applnum;
                let para = {
                    applnum: is_applnum
                };
                if(is_applnum != null && is_applnum != "") {
                    api.findMaxAgntcoyByApplnum(para).then(res => {
                        this.$store.dispatch('loading/CHANGE_LOADING',false)
                        if(res.resultCode == constant.SUCCESS){
                            if(res.data.length != 1) {
                                this.disabledAdd = false;
                                this.message('error', '访问数据库EUAAPKY失败');
                                return ;
                            } else {
                                this.inseagnVo.ls_coy = res.data[0].agntcoy;
                            }
                        }
                    }).catch(err =>{
                        this.$store.dispatch('loading/CHANGE_LOADING',false);
                    })
                }


                api.findProcflgListByApplnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        data = res.data;
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库EUAAPKY失败');
                            return;
                        } else {
                            this.inseagnVo.s_statcode = res.data[0].procflg;
                            this.inseagnVo.statcode1 = res.data[0].procflg;
                            this.findClntnumListByApplnum(para);
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findClntnumListByApplnum(para) {
                api.findClntnumListByApplnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库euadpky失败');
                            return;
                        } else {
                            this.inseagnVo.s_cltnum = res.data[0].clntnum;
                            this.findSurnameCltsexListByClntnum(this.inseagnVo.s_cltnum);
                        }

                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findSurnameCltsexListByClntnum(s_cltnum) {
                let para = {
                    clntnum: s_cltnum
                }
                api.findSurnameCltsexListByClntnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库clnt失败');
                            return;
                        } else {
                            this.inseagnVo.s_surname = res.data[0].surname;
                            this.inseagnVo.s_cltsex = res.data[0].cltsex;
                            this.findMortclsAnbatccdListByApplnum();
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findMortclsAnbatccdListByApplnum() {
                let para = {
                    applnum: this.inseagnVo.is_applnum,
                    clntnum: this.inseagnVo.s_cltnum
                }
                api.findMortclsAnbatccdListByApplnum(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问数据库euadpky失败');
                            return;
                        } else {
                            this.inseagnVo.s_mortcls = res.data[0].mortcls;
                            this.inseagnVo.s_anb_at_ccd = res.data[0].anbAtCcd;
                            this.findStatcodeListByChdrnumChdrcoy();
                        }

                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            findStatcodeListByChdrnumChdrcoy() {
                if(this.inseagnVo.s_statcode > 4) {
                    let para = {
                        chdrnum: this.searchParams.chdrnum,
                        chdrcoy: this.inseagnVo.ls_coy
                    }
                    api.findStatcodeListByChdrnumChdrcoy(para).then(res => {
                        this.$store.dispatch('loading/CHANGE_LOADING',false)
                        if(res.resultCode == constant.SUCCESS){
                            if(res.data.length != 1) {
                                this.disabledAdd = false;
                                this.message('error', '访问保单主档失败');
                                return;
                            } else {
                                this.inseagnVo.s_statcode = res.data[0].statcode;
                                this.findSAnbAtCcdListByChdrnumChdrcoy(para);
                            }
                        }
                    }).catch(err =>{
                        this.$store.dispatch('loading/CHANGE_LOADING',false);
                    })
                } else {
                    this.addAccession();
                }
            },
            findSAnbAtCcdListByChdrnumChdrcoy(para) {
                api.findSAnbAtCcdListByChdrnumChdrcoy(para).then(res => {
                    this.$store.dispatch('loading/CHANGE_LOADING',false)
                    if(res.resultCode == constant.SUCCESS){
                        if(res.data.length != 1) {
                            this.disabledAdd = false;
                            this.message('error', '访问被保人年龄失败');
                            return;
                        } else {
                            this.inseagnVo.s_anb_at_ccd = res.data[0].anbAtCcd;
                            this.addAccession();
                        }
                    }
                }).catch(err =>{
                    this.$store.dispatch('loading/CHANGE_LOADING',false);
                })
            },
            addAccession() {
              this.$router.push({path: '/home/policyRecord/reinsuranceOperation/'+this.searchParams.chdrnum,query:{
                  applnum: this.inseagnVo.is_applnum,
                  chdrnum: this.searchParams.chdrnum,
                  coy: this.inseagnVo.ls_coy,
                  statcode: this.inseagnVo.s_statcode,
                  lifcnum: this.inseagnVo.s_cltnum,
                  mortcls: this.inseagnVo.s_mortcls,
                  anbAtCcd: this.inseagnVo.s_anb_at_ccd,
                  surname: this.inseagnVo.s_surname,
                  cltsex: this.inseagnVo.s_cltsex,
                  statcode1: this.inseagnVo.statcode1
                }});
            },

            message(type, msg) {
                this.$message({
                    message: msg,
                    type: type
                });
            }


        },
        computed:{
            ...mapGetters(['tags']),
            reload(){
                return this.$store.state.loading.reload;
            }
        },
      created(){

        if(this.reload){
          this.$store.dispatch('loading/CHNAGE_RELOAD',false)
          Object.assign(this.$data, this.$options.data());
        }
      },
        activated(){
          let chdrnum=this.$route.query.chdrnum;
          if(chdrnum){
            this.searchParams.chdrnum = chdrnum;
          }

          if(this.searchParams.chdrnum || this.searchParams.surname || this.searchParams.lifcnum) {
            this.subGetList();
          }
        }
    }
</script>

<style lang="less" scoped>
    .fillcontain{
        padding-bottom: 0;
    }
    .contain{
        background: #fff;
        padding: 10px;
        margin-bottom: 20px;
    }
   .pagination{
       padding: 10px 20px;
       text-align: right;
   }
   .operationIcon{
        // padding: 12px;
        border:1px solid #e5e5e5;
        border-radius: 50%;
        width: 22px; 
        height: 22px;
        display: inline-block;
        cursor: pointer;
        box-shadow:0 0 3px #f9f9f9;
        .svg-icon{
            font-size: 14px;
            fill:#68a3fc;
        }
   }
   .search_container{
       padding: 20px 20px 0 20px;
       border-top: 1px solid #e1e1e1;
   }
</style>



